<?php
/**
 * Formular dns zaznamu
 * @author Tomas Srna
 * @package bakalarka
 */

class DnsRecordForm extends Form 
{
	public function __construct(Template $template, $values = NULL, 
		$action = NULL, $method = 'POST')
	{
		parent::__construct($template, $values, $action, $method);
		
		// name, label, [type], [class], [hint], [$options]
		$this->items = array(
			new Element('name', L::selected()->name, "text", 
						"required nospace",
						sprintf(L::selected()->dnsHint, $template->domain,
							$template->domain)
						),
			new Element('type', L::selected()->type, "select", 
						"required", "", array(
							'A'		=> 'A',
							'AAAA'	=> 'AAAA',
							'CNAME'	=> 'CNAME',
							'MX'	=> 'MX',
							'NS'	=> 'NS',
							'SOA'	=> 'SOA',
							'SPF'	=> 'SPF',
							'SRV'	=> 'SRV',
							'TXT'	=> 'TXT',
						)),
			new Element('content', L::selected()->content, "text", 
						"", "", NULL, 100),
			new Element('ttl', L::selected()->ttl, "text", 
						"numeric"),
			new Element('mx_priority', L::selected()->mx_priority, "text", 
						"numeric hide"),
			new Element('primary_ns', L::selected()->primary_ns, "text", 
						"nospace hide"),
			new Element('resp_person', L::selected()->resp_person, "text", 
						"nospace hide"),
			new Element('serial', L::selected()->serial, "text", 
						"numeric hide"),
			new Element('refresh', L::selected()->refresh, "text", 
						"numeric hide"),
			new Element('retry', L::selected()->retry, "text", 
						"numeric hide"),
			new Element('expire', L::selected()->expire, "text", 
						"numeric hide"),
			new Element('default_ttl', L::selected()->default_ttl, "text", 
						"numeric hide"),
		);
	}
	
	/**
	 * Pokial nam zakladne kontroly nestacia, mozme si pridat vlastne
	 * specificke pre dany formular
	 */
	public function validate()
	{
		parent::validate();
		
		if(!$this->getError('name') &&
			!preg_match('/^[*a-z0-9\\.-@]+$/i', 
				$this->getValue('name')))
		{
			$this->setError('name', 
				sprintf(L::selected()->invalidFormat_s, 
					'a-z, A-Z, 0-9, . - @ *'));
		}
		
		if(count($this->errors) > 0)
			return false;
		
		return true;
	}
}
